package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.CollectBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.xutils.x;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * ---收藏
 */
public class CollectActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private CollectBean bean;
    private List<CollectBean.InfoBean> infoBean = new ArrayList<>();
    private TextView classname;
//    private TextView furniture_tv;
//    private View furniture_view;
//    private View unit_bath_view;
//    private TextView unit_bath_tv;

//    private FragmentManager fragmentManager;
//    private FragmentTransaction transaction;
//
//    private FurnitureFragment furnitureFragment;
//    private UnitBathFragment unitBathFragment;
//    private List<BaseFragment> list_fragment = new ArrayList<>(); //定义要装fragment的列表
//    private int currentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);

        initData();
        initView();
//        list_fragment.clear();
//        list_fragment.add(FurnitureFragment.newInstance());
//        list_fragment.add(UnitBathFragment.newInstance());
//        fragmentManager = getSupportFragmentManager();
//        transaction = fragmentManager.beginTransaction();
//        for (int i = 0; i < list_fragment.size(); i++) {
//            transaction.add(R.id.frameLayout, list_fragment.get(i));
//            if (i != 0){
//                transaction.hide(list_fragment.get(i));
//            }
//        }
//        transaction.commit();
        //  setFragmentType();
    }

    private void initData() {
        upLoad();
    }

    private void upLoad() {
        Map<String, Object> map = new Hashtable<>();
        // 收藏列表
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.UCENTER_GETCOLLECTLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("------", result);
                Gson gson = new Gson();
                bean = gson.fromJson(result, CollectBean.class);
                infoBean = bean.getInfo();
            }

            @Override
            public void onFinished() {
                setData();
                super.onFinished();
            }
        });
    }

    private RecyclerView recycler;

    private void setData() {
        recycler.setAdapter(new BaseRVAdapter(this, infoBean) {
                                @Override
                                public int getLayoutId(int viewType) {
                                    return R.layout.collect_item;
                                }


                                @Override
                                public void onBind(BaseViewHolder holder, int position) {


                                    holder.setTextView(R.id.classname, infoBean.get(position).getClassname());
                                    RecyclerView recycler = holder.getView(R.id.item_recycler);
                                    recycler.setLayoutManager(new LinearLayoutManager(CollectActivity.this, LinearLayoutManager.VERTICAL, false));
                                    final List<CollectBean.InfoBean.KeyBean> key = infoBean.get(position).getKey();
                                    recycler.setAdapter(new BaseRVAdapter(CollectActivity.this, key) {
                                        @Override
                                        public int getLayoutId(int viewType) {
                                            return R.layout.item_collect_key;
                                        }

                                        @Override
                                        public void onBind(BaseViewHolder holder, final int position) {
                                            x.image().bind(holder.getImageView(R.id.img), key.get(position).getLogo_pic());
                                            holder.setTextView(R.id.price, "¥" + key.get(position).getPrice());
                                            holder.setTextView(R.id.goods_name, key.get(position).getGoods_name());
                                            if (key.get(position).getPrice() == null) {
                                                //整体卫浴
                                                x.image().bind(holder.getImageView(R.id.weiyu), key.get(position).getLogo_pic());
                                                holder.getView(R.id.jiaju).setVisibility(View.GONE);
                                                holder.getView(R.id.rl_weiyu).setVisibility(View.VISIBLE);
                                                holder.setTextView(R.id.weiyu_tv, key.get(position).getGoods_name());
                                                //  holder.setTextView(R.id.goods_name2, key.get(position).getGoods_name());
                                                holder.getView(R.id.max_ll).setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(CollectActivity.this, BathroomDetailActivity.class);
                                                        intent.putExtra("packageId", key.get(position).getGoods_id());
                                                        startActivity(intent);
                                                    }
                                                });
                                            } else {
                                                holder.getView(R.id.rl_weiyu).setVisibility(View.GONE);
                                                holder.getView(R.id.max_ll).setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(CollectActivity.this, ShoppingDetailsActivity.class);
                                                        intent.putExtra("goodsId", key.get(position).getGoods_id());
                                                        startActivity(intent);
                                                    }
                                                });
                                            }
                                        }
                                    });


                                }

                            }

        );
    }


    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.collect);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        //  classname= (TextView) findViewById(R.id.classname);

//        furniture_tv = (TextView) findViewById(R.id.furniture_tv);
//        furniture_view = findViewById(R.id.furniture_view);
//        unit_bath_view = findViewById(R.id.unit_bath_view);
//        unit_bath_tv = (TextView) findViewById(R.id.unit_bath_tv);

//        findViewById(R.id.unit_bath_lay).setOnClickListener(this);
//        findViewById(R.id.furniture_lay).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
//            case R.id.furniture_lay://家具馆
//                currentPosition = 0;
//                setFragmentType();
//                hideFragment(currentPosition);
//                break;
//            case R.id.unit_bath_lay://整体浴室
//                currentPosition = 1;
//                setFragmentType();
//                hideFragment(currentPosition);
//                break;


            default:
                break;
        }
    }
//
//    private void setFragmentType() {
//        switch (currentPosition){
//            case 0:
//                furniture_tv.setTextColor(getResources().getColor(R.color.main_color));
//                unit_bath_tv.setTextColor(getResources().getColor(R.color.text));
//                unit_bath_view.setVisibility(View.GONE);
//                furniture_view.setVisibility(View.VISIBLE);
//                break;
//            case 1:
//                furniture_tv.setTextColor(getResources().getColor(R.color.text));
//                unit_bath_tv.setTextColor(getResources().getColor(R.color.main_color));
//                unit_bath_view.setVisibility(View.VISIBLE);
//                furniture_view.setVisibility(View.GONE);
//                break;
//        }
//
//    }
//
//    private void hideFragment(int index) {
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//        for (int i = 0; i < list_fragment.size(); i++) {
//            if (i == index) {
//                transaction.show(list_fragment.get(i));
//            } else {
//                transaction.hide(list_fragment.get(i));
//            }
//        }
//        transaction.commit();
//    }
}
